﻿package com.media.controllers{

	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.errors.IOError;
	import com.events.MediaEvent;
	public class SWFController extends MediaController {
		private var _SWFHolder_sp:Sprite;
		private var _SWFLoader_ld:Loader;


		override public  function loadMedia(pFileURL:String):void {
			super.loadMedia(pFileURL);
			_SWFHolder_sp=new Sprite  ;
			_SWFLoader_ld=new Loader  ;
			var pLoaderInfo:LoaderInfo=_SWFLoader_ld.contentLoaderInfo;
			pLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete,false,0,true);
			pLoaderInfo.addEventListener(Event.INIT,onLoadInit,false,0,true);
			pLoaderInfo.addEventListener(Event.OPEN,onLoadStart,false,0,true);
			pLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadError,false,0,true);
			pLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onLoadProgress,false,0,true);
			_SWFHolder_sp.addChild(_SWFLoader_ld);
			applySoundTransform();
			_SWFHolder_sp.visible=false;
			try {
				_SWFLoader_ld.load(new URLRequest(mediaFile));
				dispatchEvent(new MediaEvent(MediaEvent.LOAD));
			} catch (e:Error) {
			}
		}

		private function getContent():MovieClip {
			return _SWFLoader_ld.content  as  MovieClip;
		}

		override public  function startMedia():void {
			super.startMedia();
			getContent().play();
		}
		override public  function seek(pOffset:Number):void {
			pOffset = Math.round(pOffset);
			if (playing) {
				getContent().gotoAndPlay(pOffset - 1);
			} else {
				getContent().gotoAndStop(pOffset);
			}
			super.seek(pOffset);
		}
		override public  function stopMedia():void {
			getContent().gotoAndStop(1);
			super.stopMedia();
		}
		override public  function pauseMedia(pPause:Boolean):void {
			super.pauseMedia(pPause);
			if (paused) {
				super.stopMedia();
				getContent().stop();
			} else {
				startMedia();
			}
			startTrackProgress(! paused);
		}

		override protected  function applySoundTransform():void {
			_SWFHolder_sp.soundTransform=getSoundTransform();
		}
		override protected  function startTrackProgress(pStart:Boolean):void {
			if (pStart) {
				dispatchEvent(new MediaEvent(MediaEvent.START,mediaPosition,mediaDuration));
				_SWFHolder_sp.addEventListener(Event.ENTER_FRAME,trackProgress,false,0,true);
			} else {
				dispatchEvent(new MediaEvent(MediaEvent.STOP,mediaPosition,mediaDuration));
				_SWFHolder_sp.removeEventListener(Event.ENTER_FRAME,trackProgress);
			}
		}
		override protected  function trackProgress(pEvent:Event):void {
			if (mediaPosition == mediaDuration) {
				stopMedia();
				getContent().gotoAndStop(mediaDuration);
				dispatchEvent(new MediaEvent(MediaEvent.COMPLETE,mediaPosition,mediaDuration));
			}
			super.trackProgress(pEvent);
		}

		private function onLoadInit(pEvent:Event):void {
			mediaDuration=getContent().totalFrames;
			startMedia();
			_SWFHolder_sp.visible=true;
			dispatchEvent(new MediaEvent(MediaEvent.METADATA,mediaPosition,mediaDuration,_SWFHolder_sp.width,_SWFHolder_sp.height));
			dispatchEvent(pEvent);
		}
		private function onLoadComplete(pEvent:Event):void {
			dispatchEvent(pEvent);
		}
		private function onLoadError(pEvent:Event):void {
			dispatchEvent(pEvent);
		}
		private function onLoadStart(pEvent:Event):void {
			dispatchEvent(pEvent);
		}
		private function onLoadProgress(pEvent:Event):void {
			dispatchEvent(pEvent);
		}

		override public  function get mediaPosition():Number {
			return getContent().currentFrame;
		}
		override public  function get media():Object {
			return _SWFHolder_sp;
		}



	}
}